﻿#include "mylabel.h"

MyLabel::MyLabel(QWidget* parent):QLabel(parent)
{
    zoom = 0;
    isEnlarge = false;
    isLeftMousePressMove = false;
}

MyLabel::~MyLabel()
{

}

void MyLabel::setEnlargeEnable(bool en)
{
    //    setAttribute(Qt::WA_Moved,true);
    isEnlarge = en;
    setMouseTracking(isEnlarge);
}

Qt::CursorShape MyLabel::getCursor(int pos)
{
    switch (pos) {
        case 0:
            return Qt::SizeFDiagCursor;
        case 1:
            return Qt::SizeVerCursor;
        case 2:
            return Qt::SizeBDiagCursor;
        case 3:
            return Qt::SizeHorCursor;
        case 4:
            return Qt::SizeFDiagCursor;
        case 5:
            return Qt::SizeVerCursor;
        case 6:
            return Qt::SizeBDiagCursor;
        case 7:
            return Qt::SizeHorCursor;
        case 8:
            return Qt::OpenHandCursor;
        case 9:
            return Qt::ClosedHandCursor;
        default:
            break;
    }
    return Qt::ArrowCursor;
}

void MyLabel::mouseDoubleClickEvent(QMouseEvent *event){
    Q_UNUSED(event);
    if(isEnlarge){
        // retInfo("mouseDoubleClickEvent");
        emit imageDoubleClicked();
    }
}

void MyLabel::mousePressEvent(QMouseEvent *ev)
{
    if(zoom>0){
        //移动
        setCursor(Qt::OpenHandCursor);
        if(ev->buttons() & Qt::LeftButton){
            if(!isLeftMousePressMove){
                retInfo(QString("mouse press left (%0,%1)").arg(ev->pos().x()).arg(ev->pos().y()));
                isLeftMousePressMove = true;
            }
            curPoint = ev->pos();
        }
    }
}

void MyLabel::sendImagePos(const QPoint pos){
    // retInfo(QString("image mouse move [%0,%1] %2 %3").arg(pos.x()).arg(pos.y()).arg(pos.x()-curPoint.x()).arg(pos.y()-curPoint.y()));
    emit imageMove(pos.x()-curPoint.x(),pos.y()-curPoint.y());
    curPoint = pos;
}

void MyLabel::mouseReleaseEvent(QMouseEvent *ev)
{
//    Q_UNUSED(ev);

    // if(zoom>0){
        //停止移动
        // sendImagePos(ev->pos());
    // }
    setCursor(Qt::ArrowCursor);
    isLeftMousePressMove = false;
}

void MyLabel::mouseMoveEvent(QMouseEvent *event)
{
    if(isEnlarge&&!isLeftMousePressMove){
        // retInfo(QString("MyLabel cur mouse move[%0,%1] %2").arg(event->pos().x()).arg(event->pos().y()).arg(isEnlarge));
        emit cursorMovePos(event->pos());
    }else{
        sendImagePos(event->pos());
    }
}

void MyLabel::wheelEvent(QWheelEvent *ev){
    QPoint degrees = ev->angleDelta();
    // retInfo(QString("mylabel mouse wheel x=%0,y=%1 pos x=%2 y=%3").arg(degrees.x()).arg(degrees.y()).arg(ev->pos().x()).arg(ev->pos().y()));
    emit imageZoom(degrees.y(),ev->pos().x(),ev->pos().y());
}
